﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;
using System.Data.SqlClient;

public partial class Flow_StepBind : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DBoperate().ExecuteQuery("select * from Branch", 0, 0, "branch");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DropDownList1.Items.Add(new ListItem(dr["Branch"].ToString(), dr["Dutyid"].ToString()));
            }
            this.DropDownList1.Items.Insert(0, "选择部门....");
            Bind();
            
        }
    }
    public void Bind()
    {
        DataSet dse = new DBoperate().ExecuteQuery("select * from Employee", 0, 0, "employee");
        ListBox1.DataSource = dse.Tables[0];
        ListBox1.DataTextField = "Name";
        ListBox1.DataValueField = "Employeeid";
        ListBox1.DataBind();
        if (Request.QueryString["stepid"] != null)
        {
            string strSql = "select * from Employee where Employeeid in (select UserID from FlowStepPerson where StepID=" + Request.QueryString["stepid"] + ")";
            DataSet dsp = new DBoperate().ExecuteQuery(strSql, 0, 0, "table");
            this.ListBox2.DataSource = dsp.Tables[0];
            this.ListBox2.DataTextField = "Name";
            this.ListBox2.DataValueField = "Employeeid";
            this.ListBox2.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text == "选择部门....")
        {
            Bind();
        }
        else
        {
            string Dutyid = DropDownList1.SelectedItem.Value.ToString();
            string sql = "select Name,username from Employee  where dutyid=" + Dutyid + "";

            DataSet ds = new DBoperate().ExecuteQuery(sql, 0, 0, "employee");
            ListBox1.DataSource = ds.Tables[0];
            ListBox1.DataTextField = "Name";
            ListBox1.DataValueField = "username";
            ListBox1.DataBind();
        }
        
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        bool exist = false;
        foreach (ListItem li in this.ListBox2.Items)
        {
            if (li.Value == this.ListBox1.SelectedItem.Value)
            {
                exist = true;
            }
        }
        if (exist == false)
        {
            this.ListBox2.Items.Add(new ListItem(this.ListBox1.SelectedItem.Text, this.ListBox1.SelectedItem.Value));
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ListBox2.Items.Count; i++)
        {
            if (this.ListBox2.Items[i].Selected == true)
            {
                ListBox2.Items.Remove(this.ListBox2.Items[i]);

            }
          
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        foreach (ListItem li in ListBox1.Items)
        {
            bool exist = false;
            foreach (ListItem lised in ListBox2.Items)
                if (lised.Value == li.Value)
                    exist = true;
            if (exist == false)
                ListBox2.Items.Add(new ListItem(li.Text, li.Value));
        }

    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ListBox2.Items.Count; )
        {
            ListBox2.Items.Remove(ListBox2.Items[i]);
        }
    }
    protected void btnSure_Click(object sender, EventArgs e)
    {
        int stepid = Convert.ToInt32(Request.QueryString["stepid"]);
        for (int i = 0; i < this.ListBox2.Items.Count; i++)
        {
            string strSql = "select * from FlowStepPerson where UserID="+ListBox2.Items[i].Value+" and StepID="+stepid;
            //int result =(int) new DBoperate().ExecuteScalar(strSql);
            //DataSet ds = new DBoperate().ExecuteQuery("select * from FlowStepPerson where StepID=" + stepid);
            DataSet ds = new DBoperate().ExecuteQuery(strSql,0,0,"table1");

            if (ds.Tables[0].Rows.Count<1)
            {
                int re = new DBoperate().ExecuteUpdate("insert into FlowStepPerson values("+stepid+","+ListBox2.Items[i].Value+",0,0)");
                if (re > 0)
                {
                    Response.Write("<script >window.location.href='FlowStep.aspx?id="+Request.QueryString["flowid"].ToString()+"'</script>");
                }
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.location.href='FlowStep.aspx?id=" + Request.QueryString["flowid"].ToString() + "'</script>");
    }
}
